$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tblmtlitem/list',
        datatype: "json",
        colModel: [			
			{ label: 'itemId', name: 'itemId', index: 'item_id', width: 50, key: true ,hidden:true},
			{ label: '物料代码', name: 'itemCode', index: 'item_code', width: 80 }, 			
			{ label: '物料名称', name: 'itemName', index: 'item_name', width: 80 }, 			
			{ label: '物料规格', name: 'itemSpecification', index: 'item_specification', width: 80 }, 			
			{ label: '单位', name: 'unit', index: 'unit', width: 80 }, 			
			{ label: '启用FLAG', name: 'enableFlag', index: 'enable_flag', width: 80 }, 			
			{ label: '物料分类', name: 'itemGroup', index: 'item_group', width: 80 }, 			
			{ label: '价格', name: 'price', index: 'price', width: 80 }, 			
			{ label: '物料图片路径', name: 'picPath', index: 'pic_path', width: 80 ,formatter:function(cellvalue,options,rowObject){
				return '<div class="page" style="width: 80px;height: 50px">'+
						'<div class="imgbox">'+
						'<a class="preview" title="点击看大图" href="../../'+cellvalue+'">'+
						'<img alt="" src="../../'+cellvalue+'" style="width: 80px;height: 50px"></a>'+
						'</div></div>';
				}},
			{ label: '公司', name: 'companyName', index: 'companyName', width: 80 },
			{ label: '备注', name: 'attribute1', index: 'attribute1', width: 80 ,hidden:true},
			{ label: '备注', name: 'attribute2', index: 'attribute2', width: 80 ,hidden:true},
			{ label: '备注', name: 'attribute3', index: 'attribute3', width: 80 ,hidden:true},
			{ label: '备注', name: 'attribute4', index: 'attribute4', width: 80 ,hidden:true},
			{ label: '备注', name: 'attribute5', index: 'attribute5', width: 80 ,hidden:true}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    $("#file").change(function (e) {
        var file = e.target.files[0] || e.dataTransfer.files[0];
        $('#photoCover').val(document.getElementById("file").files[0].name);
        if (file) {
            var reader = new FileReader();
            reader.onload = function () {
                $("img").attr("src", this.result);
            }

            reader.readAsDataURL(file);
        }
    });
    /*function saveUser2() {
        var id = $("#id").val().trim();
        var uname = $("#uname").val().trim();
        var pwd = $("#pwd").val().trim();
        var file = document.getElementById("file").files[0];
        var formData = new FormData();
        formData.append('id', id);
        formData.append('uname', uname);
        formData.append('pwd', pwd);
        formData.append('file', file);

        $.ajax({
            url: "/home/about",
            type: "post",
            data: formData,
            contentType: false,
            processData: false,
            mimeType: "multipart/form-data",
            success: function (data) {
                console.log(data);
                $("#uploadImg").attr("src",data);
            },
            error: function (data) {
                console.log(data);
            }
        });
    }*/
});

function saveUser2() {
	var name=$("#itemName").val();
    var file = document.getElementById("file").files[0];
    var formData = new FormData();
    formData.append('image', file);
    formData.append('name',name);

    $.ajax({
        url:baseURL+"sys/tblmtlitem/uploadImg",
        type: "post",
        data: formData,
        dataType: "json",
        contentType: false,
        processData: false,
        mimeType: "multipart/form-data",
        success: function (data) {
            console.log(data);
            $("#picPath").val(data.responseText);
            alert("上传成功");
        },
        error: function (data) {
            console.log(data);
            $("#picPath").val(data.responseText);
        }
    });
}

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            itemCode: null,
            itemName: null
        },
		showList: true,
		title: null,
		tblMtlItem: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tblMtlItem = {};
		},
		update: function (event) {
			var itemId = getSelectedRow();
			if(itemId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(itemId)
		},
		saveOrUpdate: function (event) {
			var url = vm.tblMtlItem.itemId == null ? "sys/tblmtlitem/save" : "sys/tblmtlitem/update";
			var picPath=$("#picPath").val();
			if(picPath==null||picPath==''){
				alert("请先上传图片");
			}
			vm.tblMtlItem.picPath=picPath;
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.tblMtlItem),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
                            $("img").attr("src", '');
                            $('#photoCover').val('');
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var itemIds = getSelectedRows();
			if(itemIds == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/tblmtlitem/delete",
                    contentType: "application/json",
				    data: JSON.stringify(itemIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(itemId){
			$.get(baseURL + "sys/tblmtlitem/info/"+itemId, function(r){
                vm.tblMtlItem = r.tblMtlItem;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                	'itemCode': vm.q.itemCode,
                	'itemName': vm.q.itemName
				},
                page:page
            }).trigger("reloadGrid");
		}
	}
});